package com.example.pilipili.interceptor;

import com.example.pilipili.exception.NoLoginException;
import com.example.pilipili.utils.JwtU;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class SessionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        HttpSession session = request.getSession() ;
        String token = (String) session.getAttribute("token") ;
        if (token == null || !JwtU.checkSign(token)) {
            throw new NoLoginException("请先登录") ;
        }
        return true;
    }
}